package com.hyjx.framework.component.message;

import com.hyjx.framework.service.UserCenterService;
import com.hyjx.framework.uc.user.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 消息控制器
 *
 * @author Peter
 */
@Controller
@RequestMapping("/message")
public class MessageController {
    @Resource
    private MessageService service;
    @Resource
    private UserService userservice;

    /**
     * 进入到信息查询页
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = RequestMethod.GET)
    public String gotoList(HttpServletRequest request) {
        return "/framework/message/list";
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList(MessageModel model, HttpServletRequest request) {
        return service.queryList(model);
    }

    /**
     * 跳转到增加页
     *
     * @return
     */
    @RequestMapping(value = "/gotoAdd", method = RequestMethod.GET)
    public String gotoAdd(HttpServletRequest request) {
        return "/message/add";
    }

    /**
     * 增加信息
     *
     * @param MessageModel
     * @return
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public String doAdd(MessageModel model) {
        if (service.add(model)) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 跳转到修改页
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = RequestMethod.GET)
    public ModelAndView gotoEdit(HttpServletRequest request) {
        String id = request.getParameter("id");
        MessageModel model = service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/framework/message/edit");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 修改信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doEdit", method = RequestMethod.POST)
    @ResponseBody
    public String doEdit(MessageModel model) {
        if (service.edit(model) > 0) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 删除信息
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoDelete", method = RequestMethod.GET)
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (service.delete(ids) > 0) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 信息查看
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = RequestMethod.GET)
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        MessageModel model = service.getInfoById(id);
//		UserModel um = userservice.getUserById(model.getFromuser());
//		if(um!=null)
//			model.setFromuser(um.getUser_name());
//		else
//			model.setFromuser(null);
        MessageUserModel mu = new MessageUserModel();
        mu.setMuid(model.getMuid());
        mu.setIsread("1");
        service.updateMsgIsread(mu);
        ModelAndView mav = new ModelAndView("/framework/message/view");
        mav.addObject("model", model);
        return mav;
    }

    @RequestMapping(value = "/refreshMsgCount", method = RequestMethod.POST)
    @ResponseBody
    public String refreshMsgCount(HttpServletRequest request) {
        return service.refreshMsgCount(UserCenterService.getCurrentUserInfo().getUser_id());
    }


}